<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品添加页面</title>
    <script src="asserts/plugins/jquery/jquery-3.6.0.min.js"></script>
    <script src="asserts/plugins/bootstrap/js/bootstrap.js"></script>
    <link href="asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script>
        $(document).ready(function() {
            $('#addGoodsForm').on('submit', function(event) {
                event.preventDefault(); // 阻止表单默认提交

                var formData = new FormData(this); // 创建表单数据对象

                formData.append('type', 'addGoods');
                for(var pair of formData.entries()) {
                    console.log(pair[0]+ ', '+ pair[1]);
                }
                $.ajax({
                    type: 'POST',
                    url: '/admin/AdminController',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        var response1 = JSON.parse(response);
                        if (response1.status === '200' || response1.status === 200) {
                            alert('商品添加成功');
                            location.reload(); // 刷新页面
                        } else {
                            alert('商品添加: ' + response1.message);
                        }
                    },
                    error: function(xhr, status, error) {
                        alert('商品添加失败: ' + error);
                    }
                });
            });
        });
    </script>
</head>
<body>
<div class="row" style="margin-left: 20px;">
    <form id="addGoodsForm" enctype="multipart/form-data">
        <div>
            <h3>新增商品</h3>
        </div>
        <hr />
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group form-inline">
                    <label>名称:</label>
                    <input type="text" name="name" class="form-control" />
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group form-inline">
                    <label>价格:</label>
                    <input type="text" name="price" class="form-control" />
                </div>
                <div class="form-group form-inline">
                    <label>评分:</label>
                    <input type="text" name="star" class="form-control" />
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-10">
                <div class="form-group form-inline">
                    <label>商品图片</label>
                    <input type="file" name="picture" />
                </div>
                <div class="form-group">
                    <label>商品简介</label>
                    <textarea name="intro" class="form-control" rows="5"></textarea>
                </div>
                <div class="form-group form-inline">
                    <input type="submit" value="添加" class="btn btn-primary" />
                    <input type="reset" value="重置" class="btn btn-default" />
                </div>
            </div>
        </div>
    </form>
</div>
</body>
</html>
